'''
Author: mypx
Date: 2025-10-21 15:29:17
LastEditTime: 2025-10-21 15:33:10
LastEditors: mypx mypx_coder@163.com
Description: 
'''
# 可见性模块
from typing import Dict, List
from ..container_modules.config_container import ConfigContainerModule
from parser.config_model import ConfigItem, Condition
from parser.expression_evaluator import ExpressionEvaluator


class VisibilityModule:
    """可见性模块"""

    def __init__(self, config_container_module: ConfigContainerModule):
        self.config_container_module = config_container_module
        self.expression_evaluator = ExpressionEvaluator()
        self.visibility_conditions: Dict[str, List[Condition]] = {}

    def register_visibility_condition(self, config_item: ConfigItem) -> None:
        """注册配置项的可见性条件"""
        if config_item.properties.visible_if:
            self.visibility_conditions[config_item.id] = [
                config_item.properties.visible_if
            ]

    def check_visibility(self, config_id: str, config_values: Dict[str, any]) -> bool:
        """检查配置项的可见性"""
        conditions = self.visibility_conditions.get(config_id)
        if not conditions:
            return True  # 没有可见性条件，默认为可见

        for condition in conditions:
            condition.evaluated = self.expression_evaluator.evaluate(
                condition.expression, config_values
            )
            if not condition.evaluated:
                return False

        return True

    def update_visibility_states(
        self, config_items: List[ConfigItem], config_values: Dict[str, any]
    ) -> None:
        """更新所有配置项的可见性状态"""
        for config_item in config_items:
            is_visible = self.check_visibility(config_item.id, config_values)
            config_item.is_visible = is_visible
            self.config_container_module.set_container_visible(
                config_item.id, is_visible
            )
